/*
 * BanBu_ListCell
 * @description 本文件提供附近的人界面的定制cell
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供附近的人界面的定制cell
 */

#import "BanBu_ListCell.h"
#import <QuartzCore/QuartzCore.h>
#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"
#import "AppDataManager.h"
#define xPo 90
#define featureEndx 310
#define sep 2.0

#define boyColor [UIColor colorWithRed:48.0/255 green:169.0/255 blue:217.0/255 alpha:1.0]
#define girlColor [UIColor colorWithRed:252.0/255 green:192.0/255 blue:213.0/255 alpha:1.0]


@implementation BanBu_ListCell

@synthesize features = _features;

- (void)dealloc
{
    _nameLabel = nil;
    _relationship = nil;
    _sexImgV = nil;
    _ageLabel = nil;
    _verify = nil;
    _distanceLabel = nil;
    _signatureLabel = nil;
    _distanceBack = nil;
    _lastTimeLabel = nil;
    _iconView = nil;
    _greenBall = nil;
    _vipImageView = nil;
    _lineview = nil;
    [_features release],_features = nil;
    [super dealloc];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //        UIImageView *bgColorView = [[UIImageView alloc] initWithImage:[ThemeManager getThemeImage:@"backColor.png"]];
        //        bgColorView.layer.masksToBounds = YES;
        //        self.selectedBackgroundView = bgColorView;
        
        // cell背景
        BanBu_ImageView *backImage = [[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeCell];
        backImage.frame = CGRectMake(0, 0, 320, 88);
        self.backgroundView = backImage;
        [backImage release];
        
        //cell分割线
        _lineview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 83, 320, 1.0)];
        _lineview.image = [ThemeManager getThemeImage:@"lineColor.png"];
        [self.contentView addSubview:_lineview];
        [_lineview release];
        
        
        //头像
        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(6,7, 69, 69)];
        _iconView.layer.cornerRadius = 34.5f;
        _iconView.layer.masksToBounds = YES;
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
		[self.contentView addSubview:_iconView];
        [_iconView release];
        
        //在线标志
        _greenBall = [[UIImageView alloc] initWithFrame:CGRectMake(65, 5, 10, 10)];
        _greenBall.image = [UIImage imageNamed:@"onlineFlag.png"];
        _greenBall.hidden = YES;
        [self.contentView addSubview:_greenBall];
        [_greenBall release];
        
        //vip标志
        _vipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 6, 71, 73)];
        
        [self.contentView addSubview:_vipImageView];
        [_vipImageView release];
        
        //名字
		_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPo,5, 140, 20)];
		_nameLabel.textAlignment = kTextAlignmentLeft;
		_nameLabel.backgroundColor = [UIColor clearColor];
		_nameLabel.font = [UIFont boldSystemFontOfSize:16];
		[self.contentView addSubview:_nameLabel];
        [_nameLabel release];
        
        //关系：关注or好友
        _relationship = [[UIImageView alloc] initWithFrame:CGRectMake(154, 8, 14, 14)];
        
        [self.contentView addSubview:_relationship];
        [_relationship release];
        
		//年龄和性别
        _sexImgV = [[UIImageView alloc]initWithFrame:CGRectMake(145, 36, 13, 13)];
        [self.contentView addSubview:_sexImgV];
        [_sexImgV release];
        
        _ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(161, 34, 50, 16)];
        _ageLabel.backgroundColor = [UIColor clearColor];
		_ageLabel.font = [UIFont systemFontOfSize:13];
		_ageLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
		[self.contentView addSubview:_ageLabel];
        [_ageLabel release];
        
        
        //视频认证用户标志
        NSString *verifypath = [[NSBundle mainBundle] pathForResource:@"verify_card" ofType:@"png"];
        _verify = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:verifypath]];
        _verify.frame = CGRectMake(188, 37.5, 15, 10);
        
        _verify.hidden = YES;
        [self.contentView addSubview:_verify];
        [_verify release];
        
        _distanceBack = [[UIImageView alloc]initWithFrame:CGRectMake(87, 36, 10, 14)];
        NSString *signatureBackpath = [[NSBundle mainBundle] pathForResource:@"distance_mark" ofType:@"png"];
        _distanceBack.image =  [UIImage imageWithContentsOfFile:signatureBackpath];
        [self.contentView addSubview:_distanceBack];
        [_distanceBack release];
        _distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 34, 47, 16)];
        _distanceLabel.textAlignment = kTextAlignmentLeft;
		_distanceLabel.backgroundColor = [UIColor clearColor];
		_distanceLabel.font = [UIFont systemFontOfSize:13];
		_distanceLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
		[self.contentView addSubview:_distanceLabel];
        [_distanceLabel release];
		
        
		_signatureLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPo,60, 220, 15)];
		_signatureLabel.backgroundColor = [UIColor clearColor];
        
		_signatureLabel.font = [UIFont systemFontOfSize:13.0f];
		_signatureLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        
		[self.contentView addSubview:_signatureLabel];
        [_signatureLabel release];
        
        /* for(int i=0; i<4; i++)
         {
         UIImageView *featureView = [[UIImageView alloc] initWithFrame:CGRectMake(featureEndx-(sep+15)*(i+1), 10, 15, 11)];
         featureView.backgroundColor = [UIColor clearColor];
         featureView.tag = i;
         [self addSubview:featureView];
         [featureView release];
         }*/
        
        _lastTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPo+160, 8, 60, 15)];
        _lastTimeLabel.backgroundColor = [UIColor clearColor];
		_lastTimeLabel.font = [UIFont systemFontOfSize:13];
        _lastTimeLabel.textAlignment = kTextAlignmentRight;
		_lastTimeLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
		[self.contentView addSubview:_lastTimeLabel];
        [_lastTimeLabel release];
        
        
        
    }
    return self;
}

#pragma mark - updateListCell:
/**
 * @函数名称：updateListCell:
 * @函数描述：根据传进来的数据显示cell上的信息
 * @输入参数：(NSDictionary *)buddy
 * @输出参数：要显示的详细信息
 * @返回值：nil
 * @author：yinlinlin
 */
- (void)updateListCell:(NSDictionary *)buddy
{
    NSDictionary * infoDict = [buddy valueForKey:keyPersonalInfo];
    NSLog(@"%@",infoDict);
    [_iconView setImageWithURL:[NSURL URLWithString:[infoDict valueForKey:@"uface"]] placeholderImage:[UIImage imageNamed:@"default.png"] andType:1];
    
    [self setName:[infoDict valueForKey:@"pname"] andUserid:[buddy valueForKey:@"userid"]];
    [self setDistance:[infoDict valueForKey:@"dmeter"] timeAgo:[infoDict valueForKey:@"dtime"]];
    [self setAge:[infoDict valueForKey:@"oldyears"] sex:[infoDict valueForKey:@"gender"] verify:[[infoDict valueForKey:@"verify"] boolValue]];
    
    //签名+城市
    NSString * cityStr = [[buddy valueForKey:keyPersonalInfo] valueForKey:@"incityofname"];
    if (cityStr && cityStr.length)
    {
        [self setSignature:[NSString stringWithFormat:@"[%@] %@",cityStr,[infoDict valueForKey:@"sayme"]]];
    }
    else
    {
        [self setSignature:[infoDict valueForKey:@"sayme"]];
    }
    
    [self setLastInfo:[MyAppDataManager getTheLastOnlineTime:[infoDict valueForKey:@"ltime"]]];
    [self setGreenBallOnline:[MyAppDataManager getTheSecondLastOnlineTime:[infoDict valueForKey:@"ltime"]]];
    
    [self setVIPFlag:[[infoDict valueForKey:@"userlevel"] integerValue]>0];
}

- (void)setLastInfo:(NSString *)infoStr;
{
    _lastTimeLabel.text = infoStr;
}

- (void)setAvatar:(NSString *)avatarUrlStr
{
    [_iconView setImageWithURL:[NSURL URLWithString:avatarUrlStr] placeholderImage:[UIImage imageNamed:@"default.png"] andType:1];
}


- (void)setName:(NSString *)name andUserid:(NSString *)userid
{
    CGRect frame = _nameLabel.frame;
    CGFloat w = [name sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(160, 20) lineBreakMode:kTextLineBreakByWordWrapping].width;
    if(w > 140)
    {
        w = 140;
    }
    frame.size.width = w;
    _nameLabel.frame = frame;
    _nameLabel.text = name;
    
    NSInteger _kind; //0关注2好友1粉丝3黑名单4陌生人
    NSString *fensi = [[[UserDefaults valueForKey:MyAppDataManager.useruid]valueForKey:FriendShip] valueForKey:@"f"];
    NSString *guanzhu = [[[UserDefaults valueForKey:MyAppDataManager.useruid]valueForKey:FriendShip] valueForKey:@"g"];
    NSString *friend = [[[UserDefaults valueForKey:MyAppDataManager.useruid]valueForKey:FriendShip] valueForKey:@"h"];
    NSString *black = [[[UserDefaults valueForKey:MyAppDataManager.useruid]valueForKey:FriendShip] valueForKey:@"x"];
    //    NSLog(@"FriendShip : %@",[[UserDefaults valueForKey:MyAppDataManager.useruid]valueForKey:FriendShip]);
    if([friend rangeOfString:userid].location != NSNotFound)
    {
        _kind = 2;//好友
    }
    //
    else if([guanzhu rangeOfString:userid].location != NSNotFound)
    {
        _kind = 0;//关注
    }
    else if([fensi rangeOfString:userid].location != NSNotFound)
    {
        _kind = 1;//粉丝
    }
    else if([black rangeOfString:userid].location != NSNotFound)
    {
        _kind = 3;//黑名单
    }
    else
    {
        _kind = 4;//陌生人
    }
    
    NSString *picName = nil;
    if(_kind ==0)
    {
        picName = @"follow";
    }
    else if(_kind == 2)
    {
        picName = @"friend";
    }
    
    if(picName)
    {
        NSString *relationBackpath = [[NSBundle mainBundle] pathForResource:picName ofType:@"png"];
        _relationship.image =  [UIImage imageWithContentsOfFile:relationBackpath];
        _relationship.hidden = NO;
        CGRect _relFrame = _relationship.frame;
        _relFrame.origin.x = frame.origin.x + frame.size.width + 5;
        _relationship.frame = _relFrame;
    }
    else
    {
        _relationship.hidden = YES;
    }
    
    
    
    
}

//-(void)drawRect:(CGRect)rect{
//    UIImage * imagePic=[UIImage imageNamed:[NSString stringWithFormat:@"%@.png",_sexString]];
//    [imagePic drawInRect:CGRectMake(xPo, 35, 28, 14)];
//}

- (void)setAge:(NSString *)age sex:(NSString *)sex verify:(BOOL)verify
{
    [_verify setHidden:!verify];
    if (!verify)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"verify_normal" ofType:@"png"];
        _vipImageView.image =  [UIImage imageWithContentsOfFile:path];
        
    }
    else
    {
        
        NSString * verifySource = [NSString stringWithFormat:@"verify_%@",sex];
        NSString *path = [[NSBundle mainBundle] pathForResource:verifySource ofType:@"png"];
        _vipImageView.image =  [UIImage imageWithContentsOfFile:path];
    }
    
    NSString *sexpath = [[NSBundle mainBundle] pathForResource:sex ofType:@"png"];
    _sexImgV.image =  [UIImage imageWithContentsOfFile:sexpath];
    _ageLabel.text = age;
    [_sexImgV setFrame:CGRectMake(100 + _distanceLabel.frame.size.width + 10, 36, 13, 13)];
    [_ageLabel setFrame:CGRectMake(116 + _distanceLabel.frame.size.width + 10, 34, 50, 16)];
}

- (void)setDistance:(NSString *)disStr timeAgo:(NSString *)timeStr
{
    _distanceLabel.text = disStr;
    [_distanceLabel sizeToFit];
    [_verify setFrame:CGRectMake(150 + _distanceLabel.frame.size.width, 37.5, 15, 10)];
    //    _distanceLabel.text = [NSString stringWithFormat:@"%@ | %@",disStr,timeStr];
}

- (void)setSignature:(NSString *)signature
{
    if (signature.length > 1)
    {
        _signatureLabel.text = signature;
        [_signatureLabel setHidden:NO];
        
        
    }
    else
    {
        [_signatureLabel setHidden:YES];
    }
    
}



#pragma mark - 选中状态时改变部分字体颜色
- (void)layoutSubviews
{
    [super layoutSubviews];
    _lineview.image = [ThemeManager getThemeImage:@"lineColor.png"];
    //    _distanceBack.image = [ThemeManager getThemeImage:@"distanceBack.png"];
    if(self.highlighted)
    {
        _nameLabel.textColor = [UIColor whiteColor];
        _signatureLabel.textColor = [UIColor whiteColor];
        _distanceLabel.textColor = [UIColor whiteColor];
        _lastTimeLabel.textColor = [UIColor whiteColor];
        _ageLabel.textColor = [UIColor whiteColor];
    }
    else
    {
        _nameLabel.textColor = [UIColor blackColor];
        _signatureLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        _distanceLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        _lastTimeLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        _ageLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
    }
    //    else
    //    {
    //        _signatureLabel.textColor = [UIColor grayColor];
    //        _nameLabel.textColor = [UIColor blackColor];
    ////        _distanceLabel.textColor = [UIColor grayColor];
    //        _lastTimeLabel.textColor = [UIColor grayColor];
    //
    //    }
}


-(void)cancelImageLoad{
    
    [_iconView cancelCurrentImageLoad];
    _iconView.image = nil;
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
	[super willMoveToSuperview:newSuperview];
	if(!newSuperview) {
		[self cancelImageLoad];
	}
}

-(void)setGreenBallOnline:(NSTimeInterval)_time
{
    if(_time<=900)
    {
        _greenBall.hidden = NO;
    }
    else
    {
        _greenBall.hidden = YES;
    }
}
-(void)setVIPFlag:(BOOL)_flag
{
    if (_flag)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"verify_vip" ofType:@"png"];
        _vipImageView.image =  [UIImage imageWithContentsOfFile:path];
    }
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
}
-(void)setIfHaveMoreFaces:(BOOL)_flag
{
    //    _moreFaceImageView.hidden = !_flag;
}



@end
